import 'package:flutter/cupertino.dart';

import '../../public.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class MomentsPage extends StatefulWidget {
  @override
  _MomentsPageState createState() => _MomentsPageState();
}

class _MomentsPageState extends State<MomentsPage>
    with AutomaticKeepAliveClientMixin {
  int count = 0;

  void add() {
    setState(() {
      count++;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    add();
    print(count);
  }

  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

//  HideAlways
  @override
  Widget build(BuildContext context) {
    super.build(context);
    // TODO: implement build
    return ProviderWidget<HomeModel>(
        model: HomeModel(Provider.of<ThemeModel>(context)),
        onModelReady: (model) async => model.initData(),
        builder: (context, model, child) {
          if (model.busy) {
            return Container(
              color: model.themeModel.customThemeData.mainColor,
              child: ViewStateBusyWidget(),
            );
          } else if (model.error) {
            return ViewStateWidget(onPressed: model.initData);
          } else if (model.empty) {
            return ViewStateEmptyWidget(onPressed: model.initData);
          }
          return SafeArea(
              child: Column(
            children: <Widget>[
              Container(
                color: model.themeModel.customThemeData.mainColor,
                height: ScreenUtil().setHeight(60),
                child: CustomScrollView(
                  scrollDirection: Axis.horizontal,
                  physics: BouncingScrollPhysics(),
                  slivers: <Widget>[
                    SliverList(
                        delegate: SliverChildListDelegate(_buildSort(model)))
                  ],
                ),
              ),
              Container(
                height: ScreenUtil().setHeight(1334 - 60 - 87.8) -
                    ScreenUtil.statusBarHeight -
                    ScreenUtil.bottomBarHeight,
                color: model.themeModel.customThemeData.mainColor,
                child: SmartRefresher(
                  enablePullDown: true,
                  enablePullUp: true,
                  header: MaterialClassicHeader(
                    color: Colors.white,
                    backgroundColor: Colors.black54,
                  ),
                  footer: CustomFooter(
                    loadStyle: LoadStyle.ShowWhenLoading,
                    height: 60,
                    builder: (BuildContext context, LoadStatus mode) {
                      Widget body;
                      if (mode == LoadStatus.idle) {
                        body = Text(
                          "上拉加载",
                          style: TextStyle(
                            color: model
                                .themeModel.customThemeData.mainTextColor
                                .withOpacity(0.5),
                          ),
                        );
                      } else if (mode == LoadStatus.loading) {
                        body = ViewStateLoadingWidget(
                          color: model.themeModel.customThemeData.mainTextColor
                              .withOpacity(0.5),
                        );
                      } else if (mode == LoadStatus.failed) {
                        body = Text("加载失败!",
                            style: TextStyle(
                              color: model
                                  .themeModel.customThemeData.mainTextColor
                                  .withOpacity(0.5),
                            ));
                      } else if (mode == LoadStatus.canLoading) {
                        body = Text("松手,加载更多!",
                            style: TextStyle(
                              color: model
                                  .themeModel.customThemeData.mainTextColor
                                  .withOpacity(0.5),
                            ));
                      } else {
                        body = Text("没有更多数据了!",
                            style: TextStyle(
                              color: model
                                  .themeModel.customThemeData.mainTextColor
                                  .withOpacity(0.5),
                            ));
                      }
                      return Container(
                        height: 55.0,
                        color: model.themeModel.customThemeData.mainColor
                            .withOpacity(0.5),
                        child: Center(child: body),
                      );
                    },
                  ),
                  controller: _refreshController,
                  onRefresh: () async {
                    //我在下拉刷新时执行
                    await model.refresh();
                    // if failed,use refreshFailed()
                    _refreshController.refreshCompleted();
                  },
                  onLoading: () async {
                    //我在上滑加载更多时执行
                    await model.loadMore();
                    _refreshController.loadComplete();
                  },
                  child: ListView.builder(
                    itemBuilder: (BuildContext context, int index) {
                      return MomentsItem(item: model.list[index], model: model);
                    },
                    itemCount: model.list.length,
                    shrinkWrap: true,
                  ),
                ),
              ),
            ],
          ));
        });
  }

  List<Widget> _buildSort(HomeModel model) {
    int active = 0;
    List<String> _list = ["同城交友", "闲聊讨论", "资源分享"];
    List<Widget> _ret = [];
    for (int index = 0; index < _list.length; index++) {
      _ret.add(Container(
        alignment: Alignment.center,
        margin: EdgeInsets.fromLTRB(
          index == 0 ? ScreenUtil().setWidth(20) : ScreenUtil().setWidth(10),
          0,
          ScreenUtil().setWidth(10),
          ScreenUtil().setHeight(10),
        ),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(100)),
            border: Border.all()),
        padding: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(20),
          0,
          ScreenUtil().setWidth(20),
          0,
        ),
        child: Text(
          _list[index],
          style: TextStyle(
            fontSize: ScreenUtil().setSp(26, allowFontScalingSelf: true),
            color: active == index
                ? model.themeModel.customThemeData.mainTextColor.withRed(100)
                : model.themeModel.customThemeData.mainTextColor
                    .withOpacity(0.5),
          ),
        ),
      ));
    }
    return _ret;
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
